package org.streets.workflow.engine.internal;

import org.streets.workflow.engine.WorkflowContext;
import org.streets.workflow.engine.WorkflowEngine;
import org.streets.workflow.engine.WorkflowException;
import org.streets.workflow.engine.calendar.ICalendarService;
import org.streets.workflow.engine.condition.IConditionResolver;
import org.streets.workflow.engine.persistence.IWorkflowDAO;


/**
 * RuntimeContext是Fire workflow Engine的总线。所有的服务都挂接在这个总线上，并通过这个总线获取。<br/>
 * RuntimeContext也是业务代码调用工作流引擎的入口，通过runtimeContext.getWorkflowSession()获得IWorkflowSession 对象，
 * 然后通过IWorkflowSession调用各种工作流实例对象及其API。<br/>
 * 
 * context管理的各种服务
 *
 */
public class WorkflowContextImpl implements WorkflowContext {

    /**
     * 是否已经初始化
     */
    private boolean isInitialized = false;
    
    /**
     * 转移条件表达式解析服务
     */
    private IConditionResolver conditionResolver = null;
    
    /**
     * 实例对象存取服务
     */
    private IWorkflowDAO persistenceService = null;
    
    
    /**
     * 日历服务，
     */
    private ICalendarService calendarService = null;
    
    /**
     * 是否打开流程跟踪，如果打开，则会往T_FF_HIST_TRACE表中插入纪录。
     */
    private boolean enableTrace = false;
    
    public WorkflowContextImpl() {

    }

    public Object getBeanByName(String beanName) {
    	// TODO spring
    	return null;
    }

    
    public IConditionResolver getConditionResolver() {
        return conditionResolver;
    }

    
    public void setConditionResolver(IConditionResolver resolver) {
        this.conditionResolver = resolver;
//        if (this.conditionResolver instanceof IRuntimeContextAware) {
//            ((IRuntimeContextAware) this.conditionResolver).setRuntimeContext(this);
//        }
    }

    
    public IWorkflowDAO getPersistenceService() {
        return this.persistenceService;
    }

    
    public void setPersistenceService(IWorkflowDAO persistenceService) {
        this.persistenceService = persistenceService;
    }

    
    public ICalendarService getCalendarService() {
        return calendarService;
    }

    
    public void setCalendarService(ICalendarService calendarService) {
        this.calendarService = calendarService;
        //this.calendarService.setRuntimeContext(this);
    }

    
    public WorkflowEngine getWorkflowEngine() {
        return new WorkflowEngineImpl(this);
    }

    
    public boolean isInitialized() {
        return isInitialized;
    }

    public void initialize() throws WorkflowException {
        if (!isInitialized) {
//            initAllNetInstances();
            isInitialized = true;
        }
    }

//    /**
//     * 初始化所有的工作流网实例
//     * @throws KernelException
//     */
//    protected void initAllNetInstances() throws WorkflowException {
//
//    }

    public boolean isEnableTrace() {
        return enableTrace;
    }

    public void setEnableTrace(boolean enableTrace) {
        this.enableTrace = enableTrace;
    }

}
